package com.qf.eduadmin.controller;

import com.qf.eduadmin.pojo.req.TbUserReq;
import com.qf.eduadmin.pojo.req.TbUserRoleReq;
import com.qf.eduadmin.pojo.resp.ResponseData;
import com.qf.eduadmin.pojo.vo.TbUser;
import com.qf.eduadmin.service.TbUserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 *
 * //在接口上加入该注解代表，必须登陆后才能访问该接口@RequiresAuthentication
 * 权限的控制注解，只有登录用户拥有value中的权限 才可以访问该接口@RequiresPermissions(value = {"/course/findAll"})
 *
 * @author Mr.Liu
 * @date 2021/12/27 19:02
 */
@RequestMapping("/user")
@RestController
public class TbUserController {
    @Resource
    private TbUserService tbUserService;

    @RequestMapping("/login")
    public ResponseData login(@RequestBody TbUserReq tbUserReq, HttpServletRequest request){
       // System.out.println(tbUserReq);
        ResponseData responseData = tbUserService.login(tbUserReq,request);
        return responseData;
    }

    @RequestMapping("/getUser")
    public ResponseData getUser(HttpServletRequest request){
        //TbUser tbuser = (TbUser) request.getSession().getAttribute("tbuser");
        //从shiro中获取用户
        Subject subject = SecurityUtils.getSubject();
        String userName = (String) subject.getPrincipal();
        if(userName!=null){
            return new ResponseData(0,"登录成功",userName,null);
        }
        return new ResponseData(1,"登录失败",null,null);
    }

    @RequestMapping("/findAll")
    @RequiresAuthentication
    @RequiresPermissions(value = {"/user/findAll"})
    public ResponseData findAll(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit){
        ResponseData responseData = tbUserService.findAll(page,limit);
        return responseData;
    }

    @RequestMapping("/deleteUserById")
    @RequiresAuthentication
    @RequiresPermissions(value = {"/user/deleteUserById"})
    public ResponseData deleteUserById(@RequestParam("id")Integer id){
        ResponseData responseData = tbUserService.deleteUserById(id);
        return responseData;
    }

    @RequestMapping("/insertUser")
    @RequiresAuthentication
    @RequiresPermissions(value = {"/user/insertUser"})
    public ResponseData insertUser(@RequestBody TbUser tbUser){
        System.out.println(tbUser);
        ResponseData responseData = tbUserService.insertUser(tbUser);
        return responseData;
    }

    /**
     * 更新用户角色
     * @param tbUserRoleReq
     * @return
     */
    @RequestMapping("/updateRole")
    @RequiresAuthentication
    @RequiresPermissions(value = {"/user/updateRole"})
    public ResponseData updateRole(@RequestBody TbUserRoleReq tbUserRoleReq){
        //System.out.println(tbUserRoleReq);
        ResponseData responseData = tbUserService.updateRole(tbUserRoleReq);
        return responseData;
    }

    @RequestMapping("/logout")
    @RequiresAuthentication
    public ResponseData logout(){
        /*Subject subject = SecurityUtils.getSubject();
        subject.logout();*/
        return new ResponseData(0,"退出成功",null,null);
    }

}
